define(['zepto', 'butterfly/view','../../bsl',
    'text!./webview_pullhead_custom.html'
], function($, View, bsl, webview_pullhead_custom) {
    var me = null;

    var view = View.extend({
        events: {

            // ！！！！ 由于 引入 touch.js 后，tap事件会变成双击，所以 本页面内不能使用tap，用click代替。
            "tap .openDoc" : "openDoc",
            "tap #back" : "back",

      
        },
        render: function() {
            me = this;
        },

        onShow: function() {
             etext=document.getElementById("text");
            eicon=document.getElementById("icon");
            this.plusReady();

            // 绑定自定义事件 滑动切换日历等           


        },
                 ws:null,list:null,
        // 扩展API加载完毕，现在可以正常调用扩展API 
        plusReady:function (){
            ws=bsl.webview.currentWebview();
            wo=ws.opener();
            list=bsl.webview.create("webview_pullrefresh_custom.html","webview_pullrefresh_custom.html",{top:"45px",bottom:"0px",bounce:"vertical"});
            ws.append(list);
            if(!ws.preate){
                list.addEventListener("loaded",function(){//叶面加载完成后才显示
                    ws.show("pop-in");
                    wo.evalJS("closeWaiting()");
                },false);
            }
            ws.append(list);
            // 实现列表可下拉刷新
            list.addEventListener("dragBounce",onPullStateChange,false);
            list.setBounce({position:{top:"100px"},changeoffset:{top:"50px"}});
        },
        // if(window.plus){
        //     plusReady();
        // }else{
        //     document.addEventListener("plusready",plusReady,false);
        // }
        // DOMContentLoaded事件处理
         etext:null,eicon:null,
        // document.addEventListener("DOMContentLoaded",function(){
        //     etext=document.getElementById("text");
        //     eicon=document.getElementById("icon");
        // },false);

        // 下拉状态改变
        onPullStateChange:function (e){
            switch(e.status){
                case "beforeChangeOffset"://下拉可刷新状态
                pull1();
                break;
                case "afterChangeOffset"://松开可刷新状态
                pull2();
                break;
                case "dragEndAfterChangeOffset"://正在刷新状态
                list.evalJS("onRefresh();");
                pull3();
                break;
                default:
                break;
            }
        },
        pull1:function (){
            etext.textContent="下拉可刷新";
            eicon.style.webkitTransition="all 0.3s ease-in";
            eicon.style.webkitTransform="rotate(0deg)";
        },
        pull2:function (){
            etext.textContent="松开可刷新";
            eicon.style.webkitTransition="all 0.3s ease-in";
            eicon.style.webkitTransform="rotate(180deg)";
        },
        pull3:function (){
            etext.textContent="正在刷新...";
            eicon.src="images/pull_fresh.png";
            eicon.style.webkitAnimation="spin 1s infinite linear";
        },
        pullReset:function (){
            etext.textContent="下拉可刷新";
            eicon.src="images/pull_arrow.png";
            eicon.style.webkitTransition="";
            eicon.style.webkitTransform="";
            eicon.style.webkitAnimation="";
        },
          back : function(e){
            window.history.go(-1);
        },
        openDoc:function(){
             window.butterfly.navigate('/doc/events.html', {
                trigger: true
            });


        },
       
             
            });
    return view;
});
